﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.ProviderBase;
using System.Data.Common;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace Code
{
    public class MemberController
    {
        MemberDataAccess _dataAccess;
        public MemberController()
        {
            _dataAccess = new MemberDataAccess();
        }

        public int GetInsertId()
        {
            String strID = "";
            List<int> list;
            list = new List<int>();
            int id = 0;

            DataTable dt = _dataAccess.GetIdList();

            if ((dt != null) && (dt.Rows.Count > 0))
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    strID = dt.Rows[i][0].ToString();
                    id = Convert.ToInt32(strID);
                    list.Add(id);
                }

                list.Sort();
                if (list[0] != 1)
                {
                    return 1;
                }
                else
                {
                    for (int i = 0; i < list.Count - 1; i++)
                    {
                        if (list[i + 1] != list[i] + 1)
                            return list[i] + 1;
                    }
                    return list[list.Count - 1] + 1;
                }
            }
            else
                return -1;
        }

        public bool AddMember(MemberInfo member)
        {
            return _dataAccess.AddMember(member);
        }

        public bool CheckUsername(String strUsername)
        {
            return _dataAccess.CheckUsername(strUsername);
        }

        public bool CheckEmail(String strEmail)
        {
            return _dataAccess.CheckEmail(strEmail);
        }

        public DataTable GetMemberInfor(int memID)
        {
            return _dataAccess.GetMemberInfor(memID);
        }

        public String GetMemberType(String strUsername)
        {
            return _dataAccess.GetMemberType(strUsername);
        }

        public DataTable GetMemberAccount(String strUsername)
        {
            return _dataAccess.GetMemberAccount(strUsername);
        }

        public DataTable GetMemberAccount(int strUsername)
        {
            return _dataAccess.GetMemberAccount(strUsername);
        }

        public int CheckMemberAccount(String strUsername, String strPassword)
        {
            DataTable dt = GetMemberAccount(strUsername);
            if (dt == null)
                return -1;
            else
            {
                if ((dt.Rows.Count != 0) && (dt.Rows[0]["MatKhau"].ToString() == strPassword))
                {
                    return (Int32)dt.Rows[0]["Id"];
                }
                else
                    return -1;
            }
        }

        public int CheckMemberAccount(int memID, String strPassword)
        {
            DataTable dt = GetMemberAccount(memID);
            if (dt == null)
                return -1;
            else
            {
                if ((dt.Rows.Count != 0) && (dt.Rows[0]["MatKhau"].ToString() == strPassword))
                {
                    return (Int32)dt.Rows[0]["Id"];
                }
                else
                    return -1;
            }
        }

        public DataTable GetMemberHistoryInfor(int memID)
        {
            return _dataAccess.GetMemberHistoryInfor(memID);
        }

        public DataTable GetMemberFavouriteInfor(int memID)
        {
            return _dataAccess.GetMemberFavouriteInfor(memID);
        }

        public void removeFavPro(int mID, int pID)
        {
            _dataAccess.removeFavPro(mID, pID);
        }

        public string[] getInfo(int mID)
        {
            return _dataAccess.getInfo(mID);
        }

        public void updateInfo(int mID,
                string sHoTen, string sDiaChi, string sSDT, string sNgaySinh, string sEmail, string sNgheNghiep)
        {
            _dataAccess.updateInfo(mID, sHoTen, sDiaChi, sSDT, sNgaySinh, sEmail, sNgheNghiep);
        }

        public int changePass(int memID, string hNewPass)
        {
            return _dataAccess.changePass(memID, hNewPass);
        }
    }
}